package com.yyx_yu.demo.demo;

import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

import java.util.List;

public class OpenJDActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_open_jd);

//        doStartApplicationWithPackageName("com.jingdong.app.mall");

//        String url = "openapp.jdmobile://virtual?params=%7B%22sourceValue%22:%220_productDetail_97%22,%22des%22:%22productDetail%22,%22skuId%22:%2210678336210%22,%22category%22:%22jump%22,%22sourceType%22:%22PCUBE_CHANNEL%22%7D";
//        Intent intent1 = new Intent();
//        intent1.setAction("android.intent.action.VIEW");
//        Uri uri = Uri.parse(url);
//        intent1.setData(uri);
//        intent1.setClassName("com.jingdong.app.mall", "com.jd.lib.productdetail.ProductDetailActivity");
//        startActivity(intent1);

        String url = "openapp.jdmobile://virtual?params={\"category\":\"jump\",\"des\":\"productDetail\",\"skuId\":\"12656917160\",\"sourceType\":\"unknown\",\"sourceValue\":\"unknown\",\"M_sourceFrom\":\"sxB-weixin\",\"msf_type\":\"click\",\"m_param\":{\"m_source\":\"0\",\"event_series\":{},\"jda\":\"122270672.14949436954641975724631.1494943695.1495508711.1495513819.3\",\"usc\":\"direct\",\"ucp\":\"-\",\"umd\":\"none\",\"utr\":\"-\",\"jdv\":\"122270672|direct|-|none|-|1494943695465\",\"ref\":\"https://item.m.jd.com/product/12656917160.html\",\"psn\":\"14949436954641975724631|3\",\"psq\":3,\"unpl\":\"\",\"pc_source\":\"\",\"mba_muid\":\"14949436954641975724631\",\"mba_sid\":\"14955138191692630847534164786\",\"mt_xid\":\"\",\"mt_subsite\":\"\"},\"SE\":{\"mt_subsite\":\"\",\"__jdv\":\"122270672|direct|-|none|-|1494943695465\",\"unpl\":\"\",\"__jda\":\"122270672.14949436954641975724631.1494943695.1495508711.1495513819.3\"}} ";
        Intent intent = new Intent();
        intent.setAction("android.intent.action.VIEW");
        Uri uri = Uri.parse(url);
        intent.setData(uri);
        intent.setClassName("com.jingdong.app.mall", "com.jingdong.app.mall.open.InterfaceActivity");
       startActivity(intent);

    }

    private void doStartApplicationWithPackageName(String packagename) {

        // 通过包名获取此APP详细信息，包括Activities、services、versioncode、name等等
        PackageInfo packageinfo = null;
        try {
            packageinfo = getPackageManager().getPackageInfo(packagename, 0);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        if (packageinfo == null) {
            return;
        }

        // 创建一个类别为CATEGORY_LAUNCHER的该包名的Intent
        Intent resolveIntent = new Intent(Intent.ACTION_MAIN, null);
        resolveIntent.addCategory(Intent.CATEGORY_LAUNCHER);
        resolveIntent.setPackage(packageinfo.packageName);

        // 通过getPackageManager()的queryIntentActivities方法遍历
        List<ResolveInfo> resolveinfoList = getPackageManager()
                .queryIntentActivities(resolveIntent, 0);

        ResolveInfo resolveinfo = resolveinfoList.iterator().next();
        if (resolveinfo != null) {
            // packagename = 参数packname
            String packageName = resolveinfo.activityInfo.packageName;
            // 这个就是我们要找的该APP的LAUNCHER的Activity[组织形式：packagename.mainActivityname]
            String className = resolveinfo.activityInfo.name;
            // LAUNCHER Intent
            Intent intent = new Intent();
            //intent.addCategory(Intent.CATEGORY_LAUNCHER);
            //className="com.jd.lib.productdetail.ProductDetailActivity";



            String url = "openapp.jdmobile://virtual?params={\"category\":\"jump\",\"des\":\"productDetail\",\"skuId\":\"12656917160\",\"sourceType\":\"unknown\",\"sourceValue\":\"unknown\",\"M_sourceFrom\":\"sxB-weixin\",\"msf_type\":\"click\",\"m_param\":{\"m_source\":\"0\",\"event_series\":{},\"jda\":\"122270672.14949436954641975724631.1494943695.1495508711.1495513819.3\",\"usc\":\"direct\",\"ucp\":\"-\",\"umd\":\"none\",\"utr\":\"-\",\"jdv\":\"122270672|direct|-|none|-|1494943695465\",\"ref\":\"https://item.m.jd.com/product/12656917160.html\",\"psn\":\"14949436954641975724631|3\",\"psq\":3,\"unpl\":\"\",\"pc_source\":\"\",\"mba_muid\":\"14949436954641975724631\",\"mba_sid\":\"14955138191692630847534164786\",\"mt_xid\":\"\",\"mt_subsite\":\"\"},\"SE\":{\"mt_subsite\":\"\",\"__jdv\":\"122270672|direct|-|none|-|1494943695465\",\"unpl\":\"\",\"__jda\":\"122270672.14949436954641975724631.1494943695.1495508711.1495513819.3\"}} ";
//        Intent intent1 = new Intent();
       intent.setAction("android.intent.action.VIEW");
        Uri uri = Uri.parse(url);
        intent.setData(uri);
//        intent1.setClassName("com.jingdong.app.mall", "com.jingdong.app.mall.open.InterfaceActivity");
//        startActivity(intent1);


            // 设置ComponentName参数1:packagename参数2:MainActivity路径
            ComponentName cn = new ComponentName(packageName, className);

            intent.setComponent(cn);
            startActivity(intent);
        }
    }
}
